package com.china08.yxyapi.api.manage.tec.teaching.lesson.topic;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.db.YxyKeChengService;
import com.china08.yxyapi.util.DateToolUtils;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLessonsTopicListController extends YxyApiBaseController {

    @Autowired
    YxyKeChengService kechengServiceImpl;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/topic/lesson/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String schoolId = getProfile(request, response).getSchoolId();
        String authorId = getProfile(request, response).getId();
        String catalogId1 = reqModel.catalogId1;
        String catalogId2 = reqModel.catalogId2;
        String catalogId3 = reqModel.catalogId3;
        String lessonStatus = reqModel.lessonStatus;
        String keyword = StringUtils.isNotBlank(reqModel.keyword) ? "%" + reqModel.keyword + "%" : "";
        Integer pageNum = reqModel.pageNum;
        Integer allPage = 0;
        List<Pojo> returnList = new ArrayList<>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map = kechengServiceImpl.topicListForTea(schoolId, authorId, catalogId1, catalogId2, catalogId3, keyword, lessonStatus, pageNum);
        if (null != map) {
            allPage = Integer.parseInt(map.get("allPage") + "");
            list = (List<Map<String, Object>>) map.get("list");
        }

        for (Map<String, Object> m : list) {
            String id = m.get("id") + "";
            String name = m.get("name") + "";
            String cover = m.get("cover") + "";
            Object obj1 = m.get("create_date");
            String count = m.get("count") + "";
            Integer duration = Integer.parseInt(m.get("duration") + "");
            String status = m.get("status") + "";
            String isVisible = m.get("is_visible") + "";
            String catalogName1 = m.get("catalogName1") + "";
            String catalogName2 = m.get("catalogName2") + "";
            String catalogName3 = m.get("catalogName3") + "";

            Pojo pojo = new Pojo();
            pojo.id = id;
            pojo.name = name;
            pojo.cover = cover;
            pojo.count = count;
            pojo.duration = disDuration(duration);
            pojo.status = status;
            pojo.isVisible = isVisible;
            if (StringUtils.isNotBlank(catalogName1) && !"null".equals(catalogName1)) {
                pojo.catalogName1 = catalogName1;
            }
            if (StringUtils.isNotBlank(catalogName2) && !"null".equals(catalogName2)) {
                pojo.catalogName2 = catalogName2;
            }
            if (StringUtils.isNotBlank(catalogName3) && !"null".equals(catalogName3)) {
                pojo.catalogName3 = catalogName3;
            }
            if (obj1 != null) {
                pojo.createDate = DateToolUtils.format((Date) obj1, "yyyy-MM-dd");
            }
            returnList.add(pojo);
        }
        RespModel respModel = new RespModel();
        respModel.allPage = allPage;
        respModel.list = returnList;
        return respModel;
    }

    public static class Pojo {
        public String id;
        public String name;
        public String cover;
        public String createDate;
        public String count;
        public String duration;
        public String status;
        public String isVisible;
        public String catalogName1 = "";
        public String catalogName2 = "";
        public String catalogName3 = "";
    }

    public static class ReqModel extends YxyApiReqModel {
        public String catalogId1;
        public String catalogId2;
        public String catalogId3;
        public String lessonStatus;
        public String keyword;
        public Integer pageNum;

        public void check() {
        }
    }

    public static class RespModel extends YxyApiRespModel {
        public Integer allPage;
        public List<Pojo> list;
    }

    public String disDuration(Integer miao) {
        if (miao == null || miao < 0) {
            miao = 0;
        }
        int h = miao / 3600;
        int m = (miao % 3600) / 60;
        int s = (miao % 3600) % 60;
        if (h > 0) {
            return (h + "时" + m + "分" + s + "秒");
        } else {
            if (m > 0) {
                return (m + "分" + s + "秒");
            } else {
                return (s + "秒");
            }
        }
    }
}
